package org.emet.jee.console.dao.security;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.emet.jee.console.entity.security.Department;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.hibernate.HibernateDao;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class DepartmentDao extends HibernateDao<Department, Long> {

	private static final Logger logger = Logger.getLogger(DepartmentDao.class);

	public boolean hasSameDeptName(Long id, String name) {
		Criteria c = createCriteria(Restrictions.eq("name", name));
		if (id != null) {
			c.add(Restrictions.ne("id", id));
		}
		Integer count = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();

		return (count != null && count > 0) ? true : false;
	}

	public Page<Department> search(Page<Department> page, String deptName) {
		if (!StringUtils.isBlank(deptName))
			return findPage(page, Restrictions.eq("name", deptName));
		return findPage(page);
	}

}
